/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.*;
import pojo.*;
import utils.HibernateUtil;

/**
 *
 * @author DANG
 */
public class HinhAnhDAO {

    public static SessionFactory _localSessionFactory;

    static {
        _localSessionFactory = (_localSessionFactory != null) ? _localSessionFactory : HibernateUtil.getSessionFactory();
    }

    public static List<Hinhanh> getHinhAnhs() throws Exception {
        List<Hinhanh> items = null;
        Session localSession = null;
        String localQuery = "FROM Hinhanh ha WHERE ha.trangThai = :statusHinhAnh ";
        try {
            //Step 01: create retrieving session using where clause.
            localSession = _localSessionFactory.openSession();
            //Step 02: begin retrieving transaction in above session.
            localSession.beginTransaction();
            //Step 03: set parameters retrieving objects.
            Query q = localSession.createQuery(localQuery);
            q.setInteger("statusHinhAnh", 1);
            items = q.list();
            //Step 04: commit and end retrieving transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getHinhAnhs", HinhAnhDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 05: close retrieving session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "getHinhAnhs", HinhAnhDAO.class.getName(), e.getMessage()));
            }
        }
        return items;
    }

    public static List<Hinhanh> getHinhAnhsByFilm(Phim film) throws Exception {
        List<Hinhanh> items = null;
        Session localSession = null;
        String localQuery = "FROM Hinhanh ha WHERE ha.phim = :film AND ha.trangThai = :statusHinhAnh ";
        try {
            //Step 01: validate input params.
            if (film == null) {
                throw new Exception("Param not valid");
            }
            //Step 02: create retrieving session using where clause.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin retrieving transaction in above session.
            localSession.beginTransaction();
            //Step 04: set parameters retrieving objects.
            Query q = localSession.createQuery(localQuery);
            q.setEntity("film", film);
            q.setInteger("statusHinhAnh", 1);
            items = q.list();
            //Step 05: commit and end retrieving transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getHinhAnhsByFilm", HinhAnhDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close retrieving session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "getHinhAnhsByFilm", HinhAnhDAO.class.getName(), e.getMessage()));
            }
        }
        return items;
    }

    public static List<Hinhanh> getHinhAnhsByCodeHinhAnh(String codeHinhAnh) throws Exception {
        List<Hinhanh> items = null;
        Session localSession = null;
        String localQuery = "FROM Hinhanh ha WHERE ha.codeHinhAnh = :codeHinhAnh AND ha.trangThai = :statusHinhAnh ";
        try {
            //Step 01: validate input params.
            if (codeHinhAnh.isEmpty()) {
                throw new Exception("Param not valid");
            }
            //Step 02: create retrieving session using where clause.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin retrieving transaction in above session.
            localSession.beginTransaction();
            //Step 04: set parameters retrieving objects.
            Query q = localSession.createQuery(localQuery);
            q.setString("codeHinhAnh", codeHinhAnh);
            q.setInteger("statusHinhAnh", 1);
            items = q.list();
            //Step 05: commit and end retrieving transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getHinhAnhsByCodeHinhAnh", HinhAnhDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close retrieving session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "getHinhAnhsByCodeHinhAnh", HinhAnhDAO.class.getName(), e.getMessage()));
            }
        }
        return items;
    }

    public static boolean insertHinhAnhs(Hinhanh paramHinhAnhs) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input params.
            if (paramHinhAnhs == null) {
                throw new Exception("Param not valid");
            }
            //Step 02: create insert session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin insert transaction in above session.
            localSession.beginTransaction();
            //Step 04: insert object into database.
            localSession.save(paramHinhAnhs);
            //Step 05: commit and end insert transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s ", "insertHinhAnhs", HinhAnhDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close delete session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s ", "insertHinhAnhs", HinhAnhDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }

    public static boolean updateHinhAnhs(Hinhanh paramHinhAnhs) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input params.
            if (paramHinhAnhs == null) {
                throw new Exception("Param not valid");
            }
            //Step 02: create update session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin update transaction in above session.
            localSession.beginTransaction();
            //Step 04: update object into database.
            localSession.update(paramHinhAnhs);
            //Step 05: commit and end update transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s ", "updateHinhAnhs", HinhAnhDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close delete session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s ", "updateHinhAnhs", HinhAnhDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }

    public static boolean deleteHinhAnhs(Hinhanh paramHinhAnhs) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input params.
            if (paramHinhAnhs == null) {
                throw new Exception("Param not valid");
            }
            //Step 02: create delete session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin delete transaction in above session.
            localSession.beginTransaction();
            //Step 04: delete object into database.
            localSession.delete(paramHinhAnhs);
            //Step 05: commit and end delete transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s ", "deleteHinhAnhs", HinhAnhDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close delete session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s ", "deleteHinhAnhs", HinhAnhDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }
}
